import { constant } from '@/utils/index'

interface Item {
    [key: string]: string;
}

// 根据提供的key, 加上域名前缀，实现图片能够访问
export const imgAddPrefix = <T extends Item, K extends keyof T>(item: T, key: K) : T => {
	const isEmpt = (v:any) => v === "" || v === null ||v === undefined
	const queryImgPrefix = () => {
		let domain = constant.IMG_PREFIX
		if(domain){
			domain = domain.replace(/:\d+$/, '');  
		}
		return domain
	}
	const IMG_PREFIX = queryImgPrefix();
	if(typeof item[key] !== 'string' || isEmpt(item[key])){
		item[key] = "/static/icon/children.png" as T[K]
		return item;
	}
    const slash = "/";
	const path = item[key].trim();
	if(path.startsWith('/')){
		item[key] = `${IMG_PREFIX}${item[key]}` as T[K];
	} else {
		item[key] = `${IMG_PREFIX}${slash}${item[key]}` as T[K];
	}
	return item
}




// 自动算出百分比
export function addPercentageToArray(arr) {  
	// 一天的秒数  
	const oneDayInSeconds = 86400;  
  
	// 遍历数组中的每个对象  
	arr.forEach(item => {  
		// 计算百分比  
		const percentage = (Number(item.useTime) / oneDayInSeconds) * 100;  
  
		// 将百分比四舍五入到小数点后两位  
		const roundedPercentage = Math.round(percentage * 100) / 100;  
  
		// 将百分比添加到当前对象中  
		item.percentage = roundedPercentage;  
	});  
  
	// 返回修改后的数组（实际上，由于JavaScript是按引用传递对象，所以原数组也被修改了）  
	return arr;  
}


// 1是下载的时候判断是否是小米手机  是   就播放小米的视频   
// 2是绑定的时候判断是否是荣耀手机   是   就播放荣耀的视频
export const mobileBrandToJumpPage = (mobileBrand: string) => {
	const xiaomi =  ["Xiaomi", "Redmi", 'xiaomi'];
	const huawie = ["HONOR"];
	if(xiaomi.includes(mobileBrand)){
		uni.navigateTo({
			url:"/pagesD/xiaomivideo/index"
		})
		return
	}
	if(huawie.includes(mobileBrand)){
		uni.navigateTo({
			url:"/pagesD/huaweivideo/index"
		})
		return
	}
}